package cn.baizhi.controller;


import cn.baizhi.entity.Clazz;
import cn.baizhi.service.ClazzService;
import cn.baizhi.vo.ClazzDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LZM
 * @since 2021-12-03
 */
@RestController
@RequestMapping("/clazzs")
public class ClazzController {

    private static final Logger log = LoggerFactory.getLogger(ClazzController.class);
    @Autowired
    private ClazzService clazzService;


    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteById(@PathVariable("id") Long id){
        clazzService.removeById(id);

        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PostMapping
    public ResponseEntity<Clazz> add(MultipartFile logo,Long tagId,String name) throws IOException {
        log.debug("logo: "+logo.getOriginalFilename());
        log.debug("tagId: "+tagId);
        log.debug("name: "+name);
        Clazz clazz = new Clazz();
        clazz.setTagId(tagId);
        clazz.setName(name);
        logo.transferTo(new File("F:\\",logo.getOriginalFilename()));
        clazz.setPath("F:\\"+logo.getOriginalFilename());

        clazzService.save(clazz);

        return new ResponseEntity<>(clazz,HttpStatus.OK);
    }

    @GetMapping
    public ResponseEntity<List<ClazzDto>> selectAll(){
        List<ClazzDto> list = clazzService.queryAll();

        return new ResponseEntity<>(list,HttpStatus.OK);

    }

    @GetMapping("/{id}")
    public ResponseEntity<Clazz> selectOne(@PathVariable("id") String id){

        Clazz clazz = clazzService.getById(id);

        return new ResponseEntity<>(clazz,HttpStatus.OK);
    }

}

